using Game.Main.Model;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class DynamoItem : WaterDispenserItem
	{
		private const int MIN_ANGLE = -60;

		private const int MAX_ANGLE = 60;

		private float ANGLE_VAL = 57.297f;

		private float touchAngle;

		private float lastTouchAngle;

		private Transform wheel;

		private float curVal;

		private float targetVal;

		private float difVal;

		private Transform pointer;

		private float curPointerAngle;

		private float waitTimer;

		private float pointerTimer;

		private bool pointerMoveLeft;

		private float liquidTimer;

		private SpriteRenderer liquidSprite;

		private int liquidColorIndex;

		private Color32[] colors = new Color32[6]
		{
			new Color32(82, 220, 180, byte.MaxValue),
			new Color32(byte.MaxValue, 151, 57, byte.MaxValue),
			new Color32(197, 176, byte.MaxValue, byte.MaxValue),
			new Color32(45, 170, 173, byte.MaxValue),
			new Color32(byte.MaxValue, 223, 87, byte.MaxValue),
			new Color32(227, 81, 82, byte.MaxValue)
		};

		private Vector2 angleDir = new Vector2(1f, 0f);

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			wheel = model.Find("Wheel");
			pointer = model.Find("Pointer");
			liquidSprite = model.Find("Liquid").GetComponent<SpriteRenderer>();
			waterSprite.color = liquidSprite.color;
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (curVal != targetVal)
			{
				difVal = curVal;
				SetWheelAngle(Mathf.MoveTowardsAngle(curVal, targetVal, Time.deltaTime * 360f));
				difVal = Mathf.Abs(difVal - curVal);
			}
			else
			{
				difVal = 0f;
			}
			if (difVal != 0f)
			{
				pointerTimer += Time.deltaTime;
				if (pointerTimer > waitTimer)
				{
					pointerTimer = 0f;
					waitTimer = Random.Range(0.8f, 1.5f);
					pointerMoveLeft = !pointerMoveLeft;
				}
				if (pointerMoveLeft)
				{
					SetPointerAngle(Mathf.Clamp(curPointerAngle - Mathf.Clamp(10f * difVal, 0f, 50f) * Time.deltaTime, -60f, 60f));
				}
				else
				{
					SetPointerAngle(Mathf.Clamp(curPointerAngle + Mathf.Clamp(10f * difVal, 0f, 50f) * Time.deltaTime, -60f, 60f));
				}
				liquidTimer += Time.deltaTime * Mathf.Clamp(difVal, 0f, 5f) / 10f;
				liquidSprite.color = Color32.Lerp(GetStartColor(), GetEndColor(), liquidTimer);
				waterSprite.color = liquidSprite.color;
				if (liquidTimer >= 1f)
				{
					liquidTimer = 0f;
					liquidColorIndex++;
				}
			}
			else if (curPointerAngle != 0f)
			{
				SetPointerAngle(Mathf.MoveTowardsAngle(curPointerAngle, 0f, 30f * Time.deltaTime));
			}
		}

		public override Color32 GetBodyColor()
		{
			if (liquidSprite != null)
			{
				return liquidSprite.color;
			}
			return base.GetBodyColor();
		}

		private Color32 GetStartColor()
		{
			if (liquidColorIndex >= colors.Length)
			{
				liquidColorIndex = 0;
			}
			return colors[liquidColorIndex];
		}

		private Color32 GetEndColor()
		{
			if (liquidColorIndex >= colors.Length - 1)
			{
				return colors[0];
			}
			return colors[liquidColorIndex + 1];
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			lastTouchAngle = CalAngle(touchEventData.curPos);
			ResetParam();
			PlayDragEffect();
			PlayDragSound();
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			StopDragEffect();
			StopDragSound();
		}

		private void ResetParam()
		{
			pointerTimer = 0f;
			waitTimer = Random.Range(0.8f, 1.5f);
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			touchAngle = CalAngle(touchEventData.curPos);
			targetVal = wheel.localEulerAngles.z + touchAngle - lastTouchAngle;
			lastTouchAngle = touchAngle;
		}

		private void SetWheelAngle(float angle)
		{
			curVal = angle;
			wheel.SetLocalRotationZ(curVal);
		}

		private void SetPointerAngle(float val)
		{
			curPointerAngle = val;
			pointer.SetLocalRotationZ(curPointerAngle);
		}

		public float CalAngle(Vector2 pos)
		{
			if (pos.y < wheel.position.y)
			{
				return 360f - Vector2.Angle(angleDir, pos - (Vector2)wheel.position);
			}
			return Vector2.Angle(angleDir, pos - (Vector2)wheel.position);
		}
	}
}
